package com.itheima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.common.exception.LeadNewsException;
import com.itheima.wemedia.fagin.WmMaterialFagin;
import com.itheima.wemedia.pojo.WmNewsMaterial;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.util.RequestContextUtil;
import com.itheima.common.vo.PageResultVo;
import com.itheima.wemedia.dto.WmMaterialPageRequestDto;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.mapper.WmMaterialMapper;
import com.itheima.wemedia.service.WmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.wemedia.service.WmNewsMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description <p>自媒体图文素材信息 业务实现</p>
 *
 * @version 1.0
 * @package com.itheima.wemedia.service.impl
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    //注入远程掉用对象
    @Autowired
    private WmMaterialFagin wmMaterialFagin;
    //注入自媒体素材引用素材信息对象
    @Autowired
    private WmNewsMaterialService wmNewsMaterialService;
    //注入配置文件的请求路径
    @Value("${url}")
    private String url;
    /**
     * 文件远程调用根据用户id查询素材数据库获取素材对象
     * @param userId 用户id
     * @return 返回素材对象
     */
    @Override
    public WmMaterial findByUserId(Long userId) {
        return query().eq("user_id", userId).one();
    }

    /**
     * 文件远程调用添加素材方法
     * @param wmMaterial 添加的素材对象
     * @return 返回boolean判断是否添加成功
     */
    @Override
    public boolean add(WmMaterial wmMaterial) {
        return this.save(wmMaterial);
    }

    /**
     * 素材分页查询
     * @param wmMaterialPageRequestDto 分页查询数据对象
     * @return 返回分页查询的数据对象
     */
    @Override
    public PageResultVo findPage(WmMaterialPageRequestDto wmMaterialPageRequestDto) {
        //创建分页条件
        IPage<WmMaterial> iPage = new Page<>(wmMaterialPageRequestDto.getPage(),wmMaterialPageRequestDto.getSize());
        //设置查询条件
        LambdaQueryWrapper<WmMaterial> lwq = new LambdaQueryWrapper<>();
        lwq.eq(wmMaterialPageRequestDto.getIsCollection()!=0,WmMaterial::getIsCollection,wmMaterialPageRequestDto.getIsCollection());
        //进行分页查询
        page(iPage,lwq);
        //遍历查询到的图片url加上请求路径
        iPage.getRecords().forEach(m->m.setUrl(url+m.getUrl()));
        //封装结果集返回给页面
        return PageResultVo.pageResult(wmMaterialPageRequestDto.getPage(),
                wmMaterialPageRequestDto.getSize(),iPage.getTotal(),iPage.getRecords());
    }
    /**
     * 素材收藏业务功能
     * @param id 素材id
     * @return 返回是否跟新成功
     */
    @Override
    public boolean updateIsCollect(Long id) {
        //获取用户id
        Long userId = RequestContextUtil.getUserId();
        //根据用户id和素材id查询数据库
        WmMaterial one = query().eq("user_id", userId).eq("id", id).one();
        //判断查询到的素材属性是否为0，为0则将其改为1
        if (one.getIsCollection()==0) {
            //将查询到的素材对象中的素材属性改为1
            one.setIsCollection(1);
        }else {
            one.setIsCollection(0);
        }
        //调用mapper层进行跟新
        boolean b = this.updateById(one);
        return b;
    }
    /**
     * 素材删除业务
     * @param id 素材id
     * @return 返回是否删除素材成功
     */
    @Override
    public  boolean deleteMaterial(Long id) {
        //根据自媒体素材id查询自媒体图片引用素材表判断是否有引用该素材
        List<WmNewsMaterial> byMaterialId = wmNewsMaterialService.getByMateriadId(id);
        //判断自媒体素材是否引用，引用了就不能删除，没有则删除数据
        if (byMaterialId!=null&&byMaterialId.size()>0){
            throw new LeadNewsException("该素材有文章引用，不能删除！");
        }
        //删除素材数据
        LambdaQueryWrapper<WmMaterial> lwq = new LambdaQueryWrapper<>();
        lwq.eq(WmMaterial::getId,id);
        //根据id查询数据库获取url对象
        WmMaterial byId = getById(id);
        //进行远程调用文件微服务删除文件
        wmMaterialFagin.deleteUrl(byId);
        //返回是否删除成功
        return remove(lwq);
    }


    /*@Override
    public boolean updateIsCollect(long id) {
        //获取用户id
        Long userId = RequestContextUtil.getUserId();
        //根据用户id和素材查询数据库
        query().eq("userId",)
        return false;
    }*/


}
